/*
 * =====================================================================================
 *
 *       Filename:  
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  06/04/14 10:01:41
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Andy (gk), andy_y_li@163.com
 *        Company:  RD
 *
 * =====================================================================================
 */


#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <malloc/malloc.h>
#include <sys/wait.h>
#include "common.h"
#include "test.h"
//#include "efi_protocol.h"
#include "menu.h"
//#include "strSpliter.h"


/*
用数组a[100]存放产生的确100个随机整数，数组x[10]来存放个位上的数字分别为
1，2，3，4，5，6，7，8，9，0的数的个数。即个位是1的个数存放在x[1]中，
个位是2的个数存放在x[2]中，……个位是0的个数存放在x[10]。 
*/
int TestRand(int argc, char *argv[]) 
{ 
    int a[101],x[11],i,p; 

    for(i=0;i<=11;i++) 
        x[i]=0; 
    for(i=1;i<=100;i++) 
    { 
        a[i]=rand() % 100; 
        printf("%4d",a[i]); 
        if(i%10==0)printf("\n"); 
    } 

    for(i=1;i<=100;i++) 
    { 
        p=a[i]%10; 
        if(p==0) p=10; 
        x[p]=x[p]+1; 
    } 
    for(i=1;i<=10;i++) 
    { 
        p=i; 
        if(i==10) p=0; 
        printf("%d,%d\n",p,x[i]); 
    } 
    printf("\n"); 
    return 0;
} 


// 求最小公倍数和最大公约数。
// 最小公倍数=两个整数之积/最大公约数
int TestMultiple(int argc, char *argv[])
{
    int m,n,mn,r;
    int ret;

    printf("please enter two data: ");
    printf("m = ");
    ret = scanf("%d",&m);
    if (ret < 0) {
        perror("scanf error");
        return -1;
    }
    printf("n = ");
    ret = scanf("%d",&n);
    if (ret < 0) {
        perror("scanf error");
        return -1;
    }

    if (m < n) {
        r = m;
        m = n;
        n = r;
    }
    mn = m * n;
    r = m % n;

    while (r != 0) {
        m = n;
        n = r;
        r = m % n;
    }
    printf("最大公约数:%d\n",n); 
    printf("最小公倍数:%d\n",mn/n); 

    return 0;
}


//把m作为被除数，将2—sqrt(m)作为除数，如果都除不尽，m就是素数，返回1；否则就不是，返回0。
int Prime(int m)
{
    int i,k;

    k = (int)sqrt(m);
    for (i=2;i<k;i++) {
        if (m % i == 0) {
            return 0;
        }
    }
    return 1;

}

int TestPrime(int argc, char *argv[])
{
    int m;
    int ret;

    printf("please enter the number: ");
    ret = scanf("%d",&m);
    if (ret < 0) {
        perror("scanf error");
        return -1;
    }

    if (Prime(m) == 1){
        printf("%d is a prime.\n",m);
    }
    else{
        printf("%d is not a prime.\n",m);
    }

    return 0;
}


//
int TestGoldbach(int argc, char *argv[])
{
    int i,x;
    int ret;

    printf("please enter the even nuimber(>6): ");
    ret = scanf("%d",&x);
    if (ret < 0) {
        perror("scanf error");
        return -1;
    }

    if (x < 6 || x % 2 !=0) {
        printf("data error!\n");
        return 1;
    }

    for(i=2;i<=x/2;i++){ 
        if (Prime(i)&&Prime(x-i)) { 
            printf("%d+%d\n",i,x-i); 
            printf("验证成功!"); 
            break; 
        } 
    }
    return 0;

}


